package com.sinosun.autotestmachine.tools.proxy;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.TaskManager;
import com.sinosun.autotestmachine.tools.ValueInfo;
import com.sinosun.autotestmachine.tools.ValueType;
import com.sinosun.autotestmachine.tools.Collector.Cache;
import com.sinosun.autotestmachine.tools.Collector.NotiSendCollector;
import com.sinosun.autotestmachine.tools.parser.ContentMaker;
import com.sinosun.autotestmachine.tools.parser.NotificationHolder;
import com.sinosun.autotestmachine.tools.parser.ParamKey;
import com.sinosun.autotestmachine.tools.proxy.callback.CallbackWrapper;
import com.sinosun.autotestmachine.tools.proxy.callback.NotificationInfo;
import com.sinosun.autotestmachine.tools.utils.Utils;
import com.sinosun.mstplib.MstpClient;
import com.sinosun.mstplib.MstpException;

import android.util.Log;

public class NotificationModule {
	
	private static NotificationHolder getNotificationHolder(String jsonStr) {
		NotificationHolder ret = new NotificationHolder();
		try {
			JSONObject jo = new JSONObject(jsonStr);
			if (jo.has(ParamKey.DESC)) {
				ret.setDescription(jo.getString(ParamKey.DESC));
			}
			if (jo.has(ParamKey.TO)) {
				String tmp = jo.getString(ParamKey.TO);
				ValueInfo vi = Utils.getUserRealInfo(tmp);
				if (vi.getValueType() == ValueType.INDEX) {
					tmp = App.getTestConfig().getUsers().get(vi.getValueContent());
				}
				ret.setTo(tmp);
			}
			if (jo.has(ParamKey.NOTIFY_CONTENT_TYPE)) {
				ret.setContentType(jo.getInt(ParamKey.NOTIFY_CONTENT_TYPE));
			}
			if (jo.has(ParamKey.NOTIFY_CONTENT_SIZE)) {
				ret.setContentSize(jo.getInt(ParamKey.NOTIFY_CONTENT_SIZE));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			ret = null;
		}
		return ret;
	}

	protected static int syncSendP2PNotification(MstpClient mstpClient, String paramJson, String cacheJson) {
		List<String> allParams = new ArrayList<String>();
		List<String> allCaches = new ArrayList<String>();
		try {
			JSONObject paramJo = new JSONObject(paramJson);
			if (paramJo.has(ParamKey.NOTIFICATION_ID)) {
				JSONArray ja = paramJo.getJSONArray(ParamKey.NOTIFICATION_ID);
				for (int i = 0; i < ja.length(); i++) {
					String tmp = ja.getString(i);
					ValueInfo vi = Utils.getMsgRealInfo(tmp);
					if (vi.getValueType() == ValueType.INDEX) {
						tmp = App.getTestConfig().getNotifyContent().get(vi.getValueContent());
					}
					allParams.add(tmp);
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			
			if (cacheJson != null) {
				JSONObject cacheJo = new JSONObject(cacheJson);
				if (cacheJo.has(ParamKey.NOTIFY_CONTENT)) {
					JSONArray ja = cacheJo.getJSONArray(ParamKey.NOTIFY_CONTENT);
					for (int i = 0; i < ja.length(); i++) {
						allCaches.add(ja.getString(i));
					}
				} else {
					return ErrorCode.ERR_PARAM_ERROR;
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		NotiSendCollector collector = new NotiSendCollector();
		for (int i = 0; i < allParams.size(); i++) {
			String ele = allParams.get(i);
			NotificationHolder get = getNotificationHolder(ele);
			String sendStr = ContentMaker.random(get.getContentSize());
			Log.e("XXX", "to mstpId is " + get.getTo());
			Log.e("XXX", "my mstpId is " + mstpClient.getMstpId());
			try {
				String notificationId = mstpClient.sendP2PNotification(sendStr, get.getTo());
				collector.addNotiID(notificationId);
				NotificationInfo newOne = new NotificationInfo();
				newOne.setFrom(mstpClient.getMstpId());
				newOne.setNotiContent(sendStr);
				newOne.setTagId(allCaches.get(i));
				Cache.allNotification.put(allCaches.get(i), newOne);
				collector.addCache(newOne);
			} catch (MstpException e) {
				e.printStackTrace();
				return ErrorCode.ERR_UNEXPECT_RESULT;
			}
		}
		CallbackWrapper.makeProxyNotificationSendCallback(collector);
		try {
			collector.waitResult();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return collector.getResult();
	}
	
	protected static int verifyRecvNotify(MstpClient mstpClient, String paramJson) {
		List<String> allNotiId = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.SHOULD_VERIFY_NOTIFY)) {
				JSONArray ja = jo.getJSONArray(ParamKey.SHOULD_VERIFY_NOTIFY);
				for (int i = 0; i < ja.length(); i++) {
					allNotiId.add(ja.getString(i));
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		NotiSendCollector collector = new NotiSendCollector();
		for (String ele : allNotiId) {
			NotificationInfo item = Cache.allNotification.get(ele);
			if (item != null) {
				item.setTagId(new String(""));
				collector.addCache(item);
			} else {
				return ErrorCode.ERR_UNEXPECT_RESULT;
			}
		}
		collector.setResult(ErrorCode.ERR_TIMEOUT_ERROR);
		CallbackWrapper.makeNotificationListener(collector);
		try {
			collector.waitResult(TaskManager.getTaskManager().getWaitTimeSec());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		CallbackWrapper.clearNoti();
		return collector.getResult();
	}
}
